<?php
/**
 * eddy
 * 会员中心 产品管理
 */

defined('IN_PHPCMS') or exit('No permission resources.');

class product {

	private $db;
	public $memberinfo;
	function __construct() {
		$this->db = pc_base::load_model('content_model');
		$this->db->set_model(13);
		$this->_userid = param::get_cookie('_userid');
		$this->_username = param::get_cookie('_username');
		$this->_groupid = param::get_cookie('_groupid');
		
		$user_db = pc_base::load_model('member_model');
		$this->memberinfo = $user_db->get_one(array('userid'=>$this->_userid));
	}
	
	/**
	 * 我的产品
	 */
	public function init()
	{
		$in = new_addslashes(array_merge($_GET, $_POST));
		
		$content_total = 0;
		$product_pages = '';
		$product_data = array();
		$ordernum = 0;
		$monthordernum = 0;
		//品牌
		$brand_db = pc_base::load_model('brand_model');

		$_brands = $brand_db->select(array('userid'=>$this->memberinfo['userid']), 'id,name,parentid', '', 'sort DESC, parentid ASC');
		$brands = array();
		$bids = '';  //品牌，分类的全部ID
		foreach ($_brands AS $v) {
			$bids .= $v['id'].',';
			if ($v['parentid'] == 0) {
				$brands[$v['id']] = $v;
			} else {
				$brands[$v['parentid']]['child'][$v['id']] = $v;
			}
		}
		if ($_GET['bid']) {
			$bids = $_GET['bid'];  //覆盖上面的
			$brand = $brand_db->get_one(array('id'=>intval($_GET['bid'])), 'id,name,parentid');
			if ($brand['parentid']==0) {
				$top_brand = $brands[$brand['id']];
				foreach ($top_brand['child'] AS $v) {
					$bids .= ','.$v['id'];
				}
			} else {
				$top_brand = $brands[$brand['parentid']];
			}
		}
		
		if ($bids) {
			$page = intval($_GET['page']) ? intval($_GET['page']) : 1;
			$pagesize = 12;
			$offset = ($page - 1) * $pagesize;
			$content_total = $this->db->count('`bids` IN('.trim($bids, ',').')');
			$product_pages = pages($content_total, $page, $pagesize);
			
			$product_data = $this->db->select('`bids` IN('.trim($bids, ',').')', 'id,title,thumb,ordernum,monthordernum', $offset.",".$pagesize, 'myhelp_inputtime DESC');
			
			$myhelp_db = pc_base::load_model('myhelp_model');
			foreach ($product_data AS &$p) {
				$ordernum += $p['ordernum'];
				$monthordernum += $p['monthordernum'];
				$p['myhelp'] = $myhelp_db->get_one(array('productid'=>$p['id']), 'id,title,inputtime', 'inputtime DESC');
			}
		}
		
		$SEO = seo(1, 0, '我的产品');
		$memberinfo = $this->memberinfo;
		include template('content', 'product_list');
	}
	
	/**
	 * 产品询价
	 */
	public function price()
	{
		$in = new_addslashes(array_merge($_GET, $_POST));
		
		$order_detail_model = pc_base::load_model('order_detail_model');
		$where = 'supplierid='.$this->memberinfo['userid'].' AND enquiry!=0';
		$total = $order_detail_model->count($where);
		$page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
		$pagesize = 12;
		$offset = ($page - 1) * $pagesize;
		
		$product_pages = pages($total, $page, $pagesize);
		
		$product_data = $order_detail_model->select($where, '*', $offset.",".$pagesize, 'updatetime DESC');
		
		//关联栏目  eddy
		$memberinfo = $this->memberinfo;

		$SEO = seo(1, 0, '产品询价');
		include template('content', 'product_price');
	}
	
	/**
	 * 编辑产品名称
	 */
	public function update_title()
	{
		$in = new_addslashes(array_merge($_GET, $_POST));
		$product = $this->db->get_one(array('id'=>$in['id']), 'bids');
		if ($product){
			$brand_db = pc_base::load_model('brand_model');
			$brand = $brand_db->get_one(array('id'=>$product['bids']), 'userid');
		}
		if (!empty($in['title']) && $in['id'] && $brand['userid']==$this->_userid) {
			$this->db->update(array('title'=>$in['title']), array('id'=>$in['id']));
		}
		die(json_encode(array('id'=>$in['id'], 'new_title'=>$in['title'])));
	}
	/**
	 * 删除
	 */
	public function delete()
	{
		$in = new_addslashes(array_merge($_GET, $_POST));
		$product = $this->db->get_one(array('id'=>$in['id']), 'id,catid,bids');
		if ($product){
			$brand_db = pc_base::load_model('brand_model');
			$brand = $brand_db->get_one(array('id'=>$product['bids']), 'userid');
		}
		$id = $product['id'];
		$categorys = getcache('category_content_'.get_siteid(),'commons');
		$catid = $product['catid'];
		$modelid = $categorys[$catid]['modelid'];
		$siteid = get_siteid();
		if ($brand['userid']==$this->_userid) {
			//删除内容
			$this->db->delete_content($id,0,$product['catid']);
			//删除统计表数据
			$this->hits_db = pc_base::load_model('hits_model');
			$this->hits_db->delete(array('hitsid'=>'c-'.$modelid.'-'.$id));
			//删除审核表数据
			$this->content_check_db = pc_base::load_model('content_check_model');
			$this->content_check_db->delete(array('checkid'=>'c-'.$id.'-'.$modelid));
			//删除推荐位数据
			$this->position_data_db = pc_base::load_model('position_data_model');
			$this->position_data_db->delete(array('id'=>$id,'catid'=>$catid,'module'=>'content'));
			//删除全站搜索中数据
			$search_model = getcache('search_model_'.$siteid,'search');
			$typeid = $search_model[$modelid]['typeid'];
			$this->search_db = pc_base::load_model('search_model');
			$this->search_db->delete_search($typeid,$id);
			//删除相关的评论
			$this->comment = pc_base::load_app_class('comment', 'comment');
			$commentid = id_encode('content_'.$catid, $id, $siteid);
			$this->comment->del($commentid, $siteid, $id, $catid);
			
			//删除求助信息
			$myhelp_db = pc_base::load_model('myhelp_model');
			$myhelp_db->delete(array('productid'=>$id));
			
			$this->db->cache_items();
			showmessage(L('operation_success'),HTTP_REFERER);
		}
	}
	
	/**
	  * 产品批量上传
	  *  eddy
	  */
	 public function batchUpload() 
	 {
	 	pc_base::load_app_func('global');
	 	$brand_db = pc_base::load_model('brand_model');
	 	$catModel = pc_base::load_model('category_model');
	 	$memberModel = pc_base::load_model('member_model');
	 	
	 	$grouplist = getcache('grouplist','member');
	 	$this->siteid = $siteid = get_siteid();
		$this->upload_url = pc_base::load_config('system','upload_url');
		$this->upload_path = pc_base::load_config('system','upload_path');		
		$this->imgext = array('jpg','gif','png','bmp','jpeg');
	 	$this->isadmin = $isadmin = 0;
		$this->siteid = $siteid = get_siteid();
		$this->swf_auth_key = $swf_auth_key = md5(pc_base::load_config('system','auth_key').SYS_TIME);
		$args = "100,jpg|jpeg|gif|png|bmp,1,,,0";
		
	 	if (isset($_POST['dosubmit'])) {
	 		$this->groupid = $_POST['groupid'];
	 		
	 		//判断权限
	 		$memberinfo = $memberModel->get_one(array('userid'=>$_POST['userid']));
			if ($memberinfo['doupload'] != 1) {
				die("0,对不起，您没有权限进行批量上传操作，请联系站点管理员开启！");
			}
			
	 		$bids = explode(',', $_POST['bids']);
			if (count($bids)>0) {
				$top_brand = $brand_db->get_one(array('id'=>$bids[0]), 'id,name,catid,parentid');
				$bid = array_pop($bids);
			} else {
				die("0,您还没有建立自己的栏目！");
			}
	 		
			if( $_POST['swf_auth_key'] != md5(pc_base::load_config('system','auth_key').$_POST['SWFUPLOADSESSID']) || ($_POST['isadmin']==0 && !$grouplist[$_POST['groupid']]['allowattachment'])) {
				die("0,您所在的会员组未开通附件上传权限，请联系管理员开通！");
			}
			if (!intval($top_brand['catid'])) {
				die("0,此品牌没有绑定栏目，不能进行上传操作！");
			}
			
			pc_base::load_sys_class('attachment','',0);
			$attachment = new attachment($_POST['module'],$_POST['catid'],$_POST['siteid']);
			$attachment->set_userid($_POST['userid']);
			$aids = $attachment->upload('Filedata',$_POST['filetype_post'],'','',array($_POST['thumb_width'],$_POST['thumb_height']),$_POST['watermark_enable']);
			if($aids[0]) {
				$filename= (strtolower(CHARSET) != 'utf-8') ? iconv('gbk', 'utf-8', $attachment->uploadedfiles[0]['filename']) : $filename ;
				if($attachment->uploadedfiles[0]['isimage']) {
					$arrtitle = explode('.', $_FILES['Filedata']['name']);
					//内容数据
					$_POST['status']	=	99;
					$_POST['style_color']	=	'';
					$_POST['style_font_weight']	=	'';
					$_POST['pc_hash']	=	$_SESSION['pc_hash'];
					
					$_POST['info'] = array(
						'monthordernum'		=>	0,
						'inputtime'			=>	date('Y-m-d H:i:s'),
						'template'			=>	'',
						'catid'				=>	$top_brand['catid'],
						'title'				=>	$arrtitle[0],
						'ordernum'			=>	0,
						'viewnum'			=>	0,
						'keywords'			=>	'',
						'goodnum'			=>	0,
						'badnum'			=>	0,
						'status'			=>	99,
						'thumb'				=>	$this->upload_url.$attachment->uploadedfiles[0]['filepath'],
						'username'			=> 	$_POST['username'],
						'bids'				=>	$bid,
					);
					
					$content_model = pc_base::load_model('content_model');
					$content_model->set_model(13);  //只针对产品模型
					$content_id = $content_model->add_content($_POST['info']);

					//更改附件状态
					$attachment_model = pc_base::load_model('attachment_model');
					$attachment_model->update(array('status'=>1),array('aid'=>$aids[0]));
					//插入附件关联内容表
					$att_index_db = pc_base::load_model('attachment_index_model');
					$att_index_db->insert(array('keyid'=>'c-'.$_POST['catid'].'-'.$content_id,'aid'=>$aids[0]));
					
					echo $aids[0].','.$this->upload_url.$attachment->uploadedfiles[0]['filepath'].','.$attachment->uploadedfiles[0]['isimage'].','.$filename;
				}		
				exit;
			} else {
				echo '0,'.$attachment->error();
				exit;
			}
		}
		
		//获取当前用户信息
		$memberinfo = $memberModel->get_one(array('userid'=>param::get_cookie('_userid')));
		if ($memberinfo['doupload'] != 1) {
			die("对不起，您没有权限进行批量上传操作，请联系站点管理员开启！");
		}
		
		//品牌
		$brand_1 = $brand_db->select(array('userid'=>$memberinfo['userid'], 'parentid'=>0), 'id,name', '', 'name ASC');
		$brand_haschild = 0;
		if (is_array($brand_1)) {
			foreach ($brand_1 as &$b){
				$b['child'] = $brand_db->select(array('userid'=>$memberinfo['userid'], 'parentid'=>$b['id']), 'id,name', '', 'name ASC');
				if ($b['child']) $brand_haschild = 1;
			}
		}
		$brands_json = json_encode($brand_1);
		
		if(!$brand_1) {
			die('您还没有添加栏目，请您联系管理员添加栏目！');
		}
		if (!$brand_haschild) {
			die('您还没有添加栏目分类，<a href="'.APP_PATH.'index.php?m=brand" target="_top">点击这里</a>添加栏目分类！');
		}
		$siteid = $_GET['siteid'];
		include template('content', 'product_batchupload');
	 }
}